/*
 * Aboboo Android
 * Copyright (C) 2012 www.aboboo.com
 * 
 */
package com.aboboo.android.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import android.widget.ViewFlipper;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.BaseAdapter;
import android.widget.AdapterView;

import com.aboboo.android.R;
import com.aboboo.android.json.ParseJsonInfo;
import com.aboboo.android.json.JsonCourseGroup;

import com.aboboo.android.activity.CoursewaresListActivity;

/**
 * 在线课件组列表,解析JSON后得到相应的信息
 * 
 * @author fangpeng
 * @version 1.0 2012-08-22
 * 
 */
public class CourseGroupsListActivity extends Activity {

	public final static String ACTIVITY_TAG = "CourseGroupsListActivity";

	// 加载数据的进度条
	private ProgressDialog pdialog;

	private ListView courseGroupListview = null;

	// private ArrayList<JsonCourseGroup> onLineCourseGroups = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.course_groups_list);

		Log.i(ACTIVITY_TAG, "switch to CourseGroupsListActivity");
		findViews();
		initValues();
	}

	private void findViews() {
		courseGroupListview = (ListView) findViewById(R.id.course_group_listview);
	}

	private void initValues() {
		pdialog = ProgressDialog.show(CourseGroupsListActivity.this, "",
				"正在获取课件组信息， 请稍候...", true, true);
		new Thread(new Runnable() {

			@Override
			public void run() {
				final ArrayList<JsonCourseGroup> onLineCourseGroups = ParseJsonInfo
						.getCourseGroups();
				Log.i(ACTIVITY_TAG, "run thread get CourseGroup info "
						+ onLineCourseGroups.size());
				CourseGroupsListActivity.this.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						if (!onLineCourseGroups.isEmpty()) {
							ArrayList<HashMap<String, Object>> courseGroupData = getCourseGroupData(onLineCourseGroups);
							courseGroupListview
									.setAdapter(new CourseGroupListViewAdapter(
											CourseGroupsListActivity.this,
											courseGroupData));
							courseGroupListview
									.setOnItemClickListener(new GroupOnItemclickListener(
											courseGroupData));
						} else {
							// todo 是否提示课件组信息加载失败
							;
						}
						pdialog.dismiss();
					}
				});
			}
		}).start();

		// String group_text = ((JsonCourseGroup) onLineCourseGroups.get(0))
		// .toString();

		// String url = ((JsonCourseGroup) onLineCourseGroups.get(0)).getUrl();
		// String courseware_text = ((JsonCourseware) ParseJsonInfo
		// .getCoursewares(url).get(0)).toString();
	}

	/**
	 * 得到要展示的课件组信息, 每个课件组的信息通过 map来封装
	 * 
	 * @return
	 */
	private ArrayList<HashMap<String, Object>> getCourseGroupData(
			final ArrayList<JsonCourseGroup> onLineCourseGroups) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (JsonCourseGroup courseGroup : onLineCourseGroups) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("img", R.drawable.img_course_group);
			map.put("name", courseGroup.getName().trim());
			map.put("coursewares", String.valueOf(courseGroup.getCoursewares()));
			map.put("members", String.valueOf(courseGroup.getMembers()));
			map.put("modified", courseGroup.getModified());
			map.put("url", courseGroup.getUrl());
			map.put("id", String.valueOf(courseGroup.getId()));

			data.add(map);
		}
		return data;
	}

	/**
	 * 课件组在列表中显示的适配器
	 * 
	 * @author fangpeng
	 * 
	 */
	private class CourseGroupListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;

		private ArrayList<HashMap<String, Object>> courseGroupData;

		public CourseGroupListViewAdapter(Context context,
				ArrayList<HashMap<String, Object>> courseGroupData) {
			this.inflater = LayoutInflater.from(context);
			this.courseGroupData = courseGroupData;
		}

		@Override
		public int getCount() {
			return courseGroupData.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = this.inflater.inflate(R.layout.course_group_item,
						null);
				holder.img = (ImageView) convertView
						.findViewById(R.id.course_group_img);
				holder.name = (TextView) convertView
						.findViewById(R.id.course_group_name);
				holder.coursewares = (TextView) convertView
						.findViewById(R.id.course_group_coursewares);
				holder.members = (TextView) convertView
						.findViewById(R.id.course_group_members);
				holder.modified = (TextView) convertView
						.findViewById(R.id.course_group_modified);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			// System.out.print(courseGroupData.get(position));

			HashMap<String, Object> groupMap = courseGroupData.get(position);
			if (groupMap != null) {
				holder.img.setBackgroundResource((Integer) groupMap.get("img"));
				holder.name.setText((String) groupMap.get("name"));
				holder.coursewares
						.setText((String) groupMap.get("coursewares"));
				holder.members.setText((String) groupMap.get("members"));
				holder.modified.setText((String) groupMap.get("modified"));
			}
			return convertView;
		}

	}

	/**
	 * 课件组View封装器
	 * 
	 * @author fangpeng
	 * 
	 */
	private final class ViewHolder {
		public ImageView img;
		public TextView name;
		public TextView coursewares;
		public TextView members;
		public TextView modified;
	}

	/**
	 * 按下单个课件组的监听器
	 * 
	 * @author fangpeng
	 * 
	 */
	private class GroupOnItemclickListener implements
			AdapterView.OnItemClickListener {
		private ArrayList<HashMap<String, Object>> courseGroupData;

		public GroupOnItemclickListener(
				ArrayList<HashMap<String, Object>> courseGroupData) {
			this.courseGroupData = courseGroupData;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			HashMap<String, Object> selectedObj = courseGroupData.get(position);
			// Toast.makeText(OnlineCourseGroupsActivity.this,
			// "你选择了:" + selectedObj.get("name").toString(),
			// Toast.LENGTH_LONG).show();
			if (selectedObj != null) {
				AbobooActivity abobooActivity = (AbobooActivity) CourseGroupsListActivity.this
						.getParent();
				ViewFlipper flipper = abobooActivity.getViewFlipper();
				flipper.removeAllViews();
				Intent intent = new Intent(CourseGroupsListActivity.this,
						CoursewaresListActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.putExtra("group_id", selectedObj.get("id").toString());
				intent.putExtra("courseGroupName", selectedObj.get("name")
						.toString());
				intent.putExtra("courseGroupUrl", selectedObj.get("url")
						.toString());
				Window activity = abobooActivity.getLocalActivityManager().startActivity(
						CoursewaresListActivity.ACTIVITY_TAG, intent);
				flipper.addView(activity.getDecorView());
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return getParent().onKeyDown(keyCode, event);
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

}
